package com.music.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.music.R;
import com.music.utilbean.MediaUtils;
import com.music.utilbean.Mp3Info;

import java.util.ArrayList;

public class MusicListAdapter extends BaseAdapter {
	private Context            ctx;
	private ArrayList<Mp3Info> mp3Infos;

	public MusicListAdapter(Context ctx, ArrayList<Mp3Info> mp3Infos) {
		this.ctx = ctx;
		this.mp3Infos = mp3Infos;
	}

	public void setMp3Infos(ArrayList<Mp3Info> mp3Infos) {
		this.mp3Infos = mp3Infos;
	}

	@Override
	public int getCount() {
		return mp3Infos.size();
	}

	@Override
	public Object getItem(int position) {
		return mp3Infos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		if (convertView == null) {
			convertView = LayoutInflater.from(ctx).inflate(R.layout.item_music_list, null);
			vh = new ViewHolder();
			vh.t_title = (TextView) convertView.findViewById(R.id.t_title);
			vh.t_singer = (TextView) convertView.findViewById(R.id.t_singer);
			vh.t_time = (TextView) convertView.findViewById(R.id.t_time);
			convertView.setTag(vh);
		}
		vh = (ViewHolder) convertView.getTag();
		Mp3Info mInfo = mp3Infos.get(position);
		vh.t_title.setText(mInfo.getTitle());
		vh.t_singer.setText(mInfo.getArtist());
		vh.t_time.setText(MediaUtils.formatTime(mInfo.getDuration()));
		return convertView;
	}

	static class ViewHolder {
		TextView t_title;
		TextView t_singer;
		TextView t_time;
	}
}
